import pandas as pd  
import statsmodels.api as sm  
from statsmodels.formula.api import ols  
  
# 创建数据框  
data = {  
    'x1': [7, 1, 11, 11, 7, 11, 3, 1, 2, 21, 1, 11, 10],  
    'x2': [26, 29, 56, 31, 52, 55, 71, 31, 54, 47, 40, 66, 68],  
    'x3': [6, 15, 8, 8, 6, 9, 17, 22, 18, 4, 23, 9, 8],  
    'x4': [60, 52, 20, 47, 33, 22, 6, 44, 22, 26, 34, 12, 12],  
    'Y': [78.5, 74.3, 104.3, 87.6, 95.9, 109.2, 102.7, 72.5, 93.1, 115.9, 83.8, 113.3, 109.4]  
}  
shuini = pd.DataFrame(data)  
  
# 用x1, x2, x3, x4作为自变量（特征）来预测Y（因变量）  
# 使用statsmodels的ols函数（普通最小二乘法）来拟合线性模型  
model = ols('Y ~ x1 + x2 + x3 + x4', data=shuini).fit()  
  
# 打印模型摘要，包括系数、R平方等  
print(model.summary())  
  
